﻿using System.Collections.Generic;
using System.Linq;
using System.Resources;
using System.Windows;
using System.Windows.Input;
using Foundation.ExtensionMethods;

namespace Foundation.MarkupExtensions
{
    public static class LocalizingManager
    {
        public static Localizing.ValueTransformer ToLower =
            (value, obj, property) => value.ToLower();

        public static Localizing.ValueTransformer ToUpper =
            (value, obj, property) => value.ToUpper();

        public static Localizing.ValueTransformer DefaultWithToLower =
            (value, obj, property) =>
            Localizing.DefaultTransformer == null
                ? value.ToLower()
                : Localizing.DefaultTransformer(value, obj, property).ToLower();

        public static Localizing.ValueTransformer DefaultWithToUpper =
            (value, obj, property) =>
            Localizing.DefaultTransformer == null
                ? value.ToLower()
                : Localizing.DefaultTransformer(value, obj, property).ToUpper();

        public static readonly DependencyProperty AttachedLocalizingProperty =
            DependencyProperty.RegisterAttached("AttachedLocalizing", typeof (Localizing), typeof (LocalizingManager),
                                                new PropertyMetadata(default(Localizing)));

        public static Localizing GetAttachedLocalizing(this DependencyObject obj)
        {
            return (Localizing) obj.GetValue(AttachedLocalizingProperty);
        }

        public static void SetAttachedLocalizing(this DependencyObject obj, Localizing value)
        {
            obj.SetValue(AttachedLocalizingProperty, value);
        }

        public static void LocalizeProperty(this DependencyObject obj, DependencyProperty property, Localizing value)
        {
            obj.SetValue(AttachedLocalizingProperty, value.ProvideValue(new SelfProvider(obj, property)));
        }

        static LocalizingManager()
        {
            Localizing.ExcludedProperties.Add(AttachedLocalizingProperty);
            Localizing.Managers.Add(Properties.Resources.ResourceManager);
            Localizing.DefaultTransformer = (value, targetObject, targetProperty) =>
                                            targetObject is ICommandSource &&
                                            targetProperty != FrameworkElement.ToolTipProperty
                                                ? value
                                                : value.Replace("_", string.Empty);
            Localizing.LocalizationChanged += () =>
                Application.Current.Windows.Cast<Window>().ToList().ForEach(w => w.UpdateBindingTargetsByVisualTree());
        }

        public static void Initialize(IEnumerable<ResourceManager> managers = null)
        {
            if (managers == null) return;
            foreach (var resourceManager in managers)
            {
                Localizing.Managers.Add(resourceManager);
            }
        }
    }
}
